package utils;

import java.io.IOException;

import org.boris.expr.Expr;
import org.boris.expr.ExprEvaluatable;
import org.boris.expr.ExprException;
import org.boris.expr.parser.ExprParser;
import org.boris.expr.util.Exprs;
import org.boris.expr.util.SimpleEvaluationContext;

public class ExperUtils {
	
	public static boolean parse(String condition){
		SimpleEvaluationContext context = new SimpleEvaluationContext();
		
		String[] andArray = condition.split("&&");
		boolean andBool = true;
		
		for(String andConditem : andArray){
			String[] orArray = andConditem.split("\\|\\|");
			
			boolean orBool = false;
			for(String orConditem : orArray){
				try {
					Expr e = ExprParser.parse(orConditem);
					
					Exprs.toUpperCase(e);
					if (e instanceof ExprEvaluatable) {
						e = ((ExprEvaluatable) e).evaluate(context);
					}
					orBool = e.toString().equals("TRUE");
					
					if(orBool) break;
					
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (ExprException e1) {
					e1.printStackTrace();
				}
			}
			
			
			if(!orBool){
				andBool = false;
				break;
			}
		}
		return andBool;
	}
}
